package com.pearadmin.pro.modules.resource.controller;

import com.pearadmin.pro.common.aop.annotation.Log;
import com.pearadmin.pro.common.constant.ControllerConstant;
import com.pearadmin.pro.common.context.UserContext;
import com.pearadmin.pro.common.tools.core.StringUtil;
import com.pearadmin.pro.common.utils.upload.entity.PathConfigurationEntity;
import com.pearadmin.pro.common.web.base.BaseController;
import com.pearadmin.pro.common.web.domain.Result;
import com.pearadmin.pro.modules.resource.BusinessProcess.MainIndex;
import com.pearadmin.pro.modules.resource.entity.ResourceEntity;
import com.pearadmin.pro.modules.resource.entity.ResourceRequest;
import com.pearadmin.pro.modules.resource.service.ResourceService;
import com.pearadmin.pro.modules.sys.domain.SysDictData;
import com.pearadmin.pro.modules.sys.domain.SysUser;
import com.pearadmin.pro.modules.sys.service.SysDictDataService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * 资源管理模块
 *
 * @author ZhouXingYun
 * @date 2022/4/18 16:23
 */
@Api(tags = {"资源管理"})
@RestController
@RequestMapping(ControllerConstant.PREFIX_SYS + "resource")
public class ResourceController extends BaseController {

    @Resource // 资源管理业务类
    private ResourceService resourceService;
    @Resource // 数据字典业务类
    private SysDictDataService sysDictDataService;
    @Resource
    private UserContext userContext;
    // 逻辑处理
    private final MainIndex mainIndex = new MainIndex();


    /**
     * 判断该用户是否有删除或者编辑该资源的权限
     *
     * @param param
     * @return com.pearadmin.pro.common.web.domain.Result
     * @author ZhouXingYun
     * @date 2022/4/22 9:22
     */
    @PostMapping("deleteResourcePermissionCheck")
    @Log(title = "删除资源权限校验")
    @ApiOperation(value = "删除资源权限校验")
    public Result deleteResourcePermissionCheck( @RequestParam("0")  String param) {
        if(StringUtil.isEmpty(param)){
            //无参数
            return failure("参数为空");
        }
        // 获取当前登陆人基本信息
        SysUser principal = userContext.getPrincipal();
        if(principal.getId().equals(param)){
            return auto(true);
        }
        return auto(false);
    }


    @GetMapping("homeImg")
    @Log(title = "首页轮播图")
    @ApiOperation(value = "首页轮播图")
    public Result homeImg() {
       List<String> stringList = resourceService.homeImg();
        return success(stringList);
    }

    @GetMapping("page")
    @Log(title = "资源列表")
    @ApiOperation(value = "资源列表")
    public Result page(ResourceRequest request) {
        System.out.println("-----------------" + PathConfigurationEntity.getUploadpath());
        return success(resourceService.page(request));
    }
    /**
     * 上传资源
     *
     * @param resourceEntity 上传资源实体类
     */
    @PostMapping("saveResource")
    @Log(title = "新增上传资源")
    @ApiOperation(value = "新增上传资源")
    public Result save(@RequestBody ResourceEntity resourceEntity) {
        if (resourceService.exist(resourceEntity.getReName())) {
            return failure("该资源名称已存在");
        }
        SysUser principal = userContext.getPrincipal();
        ResourceEntity resource = mainIndex.saveProcess(resourceEntity, principal);
        return auto(resourceService.save(resource));
    }

    /**
     * 从数据字典获取模块
     *
     * @return com.pearadmin.pro.common.web.domain.Result
     * @author ZhouXingYun
     * @date 2022/4/20 15:32
     */
    @GetMapping("getModule")
    @Log(title = "获取模块")
    @ApiOperation(value = "获取模块")
    public Result getModule() {
        List<SysDictData> list = sysDictDataService.selectModule("re_module");
        return success(list);
    }

    @DeleteMapping("remove")
    @Log(title = "资源删除")
    @ApiOperation(value = "资源删除")
    public Result remove(@RequestParam Integer id) {
        return auto(resourceService.deleteById(id));
    }

    /**
     * 资源修改
     * @author ZhouXingYun
     * @date 2022/4/22 13:57
     * @param resourceEntity 资源实体类
     * @return com.pearadmin.pro.common.web.domain.Result
     */
    @PutMapping("edit")
    @Log(title = "资源修改")
    @ApiOperation(value = "资源修改")
    public Result edit(@RequestBody ResourceEntity resourceEntity) {
        boolean a = resourceService.getReNameCount(resourceEntity.getReName(),resourceEntity.getReId());
        if (!a) {
            return failure("该资源名称已存在");
        }
        SysUser principal = userContext.getPrincipal();
        ResourceEntity resource = mainIndex.updateeProcess(resourceEntity, principal);
        return auto(resourceService.edit(resource));
    }

}
